package rs.belex.belexdroid.services.impl.ksoap2;

import java.util.LinkedList;
import java.util.List;
import java.util.Vector;

import org.ksoap2.serialization.SoapObject;

import rs.belex.belexdroid.beans.AccountBean;
import rs.belex.belexdroid.beans.ShareBean;
import rs.belex.belexdroid.beans.interfaces.Account;
import rs.belex.belexdroid.beans.interfaces.Share;
import rs.belex.belexdroid.services.interfaces.IAccountProvider;
import android.util.Log;

public class AccountProviderKsoap2Impl extends ServiceClientKsoap2Impl implements IAccountProvider {

	private static String TAG = AccountProviderKsoap2Impl.class.getSimpleName();
	
	public AccountProviderKsoap2Impl(){
	}
	
	public Account addSharesToUserAccount(String username, List<Share> shares) {
		Account account = new AccountBean();
		Log.d(TAG, "Add shares to Users Account");
		final String SOAP_ACTION = "";
		final String METHOD_NAME = "addSharesToUserAccount";

		// Creating input parameters
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
		request.addProperty("username", username);
		
		//ksoap2 requires Vector to be used as a collection so we must convert it into one
		Vector<String> symbols = new Vector<String>();
		for(Share share:shares){
			symbols.add(share.getSymbol());
		}
		
		request.addProperty("symbols", symbols);

		SoapObject response = super.requestComplexType(request, SOAP_ACTION);

		account = getAccountFromResponse(response);

		Log.d(TAG, "Add Shares To account successful");

		return account;
	}

	public Account deleteSharesFromUserAccount(String username, List<Share> shares) {
		Account account = new AccountBean();
		Log.d(TAG, "Add shares to Users Account");
		final String SOAP_ACTION = "";
		final String METHOD_NAME = "deleteSharesFromUserAccount";

		// Creating input parameters
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
		request.addProperty("username", username);
		
		//ksoap2 requires Vector to be used as a collection so we must convert it into one
		Vector<String> symbols = new Vector<String>();
		for(Share share:shares){
			symbols.add(share.getSymbol());
		}
		
		request.addProperty("symbols", symbols);

		SoapObject response = super.requestComplexType(request, SOAP_ACTION);

		account = getAccountFromResponse(response);

		Log.d(TAG, "Delete Shares From account successful");

		return account;
	}
	// re-usability of this method should be improved 
	Account getAccountFromResponse(SoapObject accountResponse) {

		SoapObject sharesResponse = (SoapObject) accountResponse.getProperty("shares");
		
		int propsNum = sharesResponse.getPropertyCount();
		List<Share> shares = new LinkedList<Share>();
		// Iterate through properties and make an array
		for (int i = 0; i < propsNum; i++) {
			SoapObject element = (SoapObject) sharesResponse.getProperty(i);
			Share share = new ShareBean();
			share.setName(element.getProperty("name").toString());
			share.setType(element.getProperty("type").toString());
			share.setSymbol(element.getProperty("symbol").toString());
			share.setValue(new Double(element.getProperty("value").toString()));
			shares.add(share);
		}
		return new AccountBean(shares);
	}
	
}
